package cn.weisoft;

import org.geotools.data.*;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.WKTReader;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.File;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );

        try {
           /* ShapefileDataStoreFactory  factory = new ShapefileDataStoreFactory();

            File file = new File("E:\\\\temp\\\\20220211\\my2.shp");
            Map<String, Serializable> params = new HashMap<>();
            params.put("url", file.toURI().toURL());
            //params.put("create spatial index", Boolean.FALSE);

            ShapefileDataStore  myData = (ShapefileDataStore)factory.createNewDataStore(params);
            SimpleFeatureType featureType =
                    DataUtilities.createType(
                            "my", "geom:Point,name:String,age:Integer,description:String");
            myData.createSchema(featureType);


            GeometryFactory geometryFactory =   myData.getGeometryFactory();
            SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);

            for(int i=0;i<10;i++){
                Point point =  geometryFactory.createPoint(new Coordinate(114+i*0.1,22+i*0.1));
                featureBuilder.add(point);
                featureBuilder.add("wilson.fu" + i);
                featureBuilder.add(30+i);
                featureBuilder.add("desc" + i);

            }

            SimpleFeature feature =  featureBuilder.buildFeature(null);*/



            String path="E:\\temp\\20220211\\my.shp";
            //1.创建shape文件对象
            File file =new File(path);
            Map<String, Serializable> params = new HashMap<String, Serializable>();

//            params.put("create spatial index", Boolean.FALSE);
//            params.put("enable spatial index", Boolean.FALSE);
            //用于捕获参数需求的数据类
            //URLP:url to the .shp file.
            params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
            //2.创建一个新的数据存储——对于一个还不存在的文件。
            ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
            //3.定义图形信息和属性信息
            //SimpleFeatureTypeBuilder 构造简单特性类型的构造器
            SimpleFeatureTypeBuilder tBuilder = new SimpleFeatureTypeBuilder();
            //设置
            //WGS84:一个二维地理坐标参考系统，使用WGS84数据
           //tBuilder.setCRS(DefaultGeographicCRS.WGS84);
            // 需要添加 gt-epsg-hsql 架包
            //tBuilder.setSRS( "4326" );

            tBuilder.setName("shapefile");
            //添加 一个点
            //tBuilder.add("the_geom", Point.class);
            tBuilder.add("the_geom", Polygon.class);
            //添加一个id
            tBuilder.add("osm_id", Long.class);
            //添加名称
            tBuilder.add("name", String.class);
            //添加描述
            tBuilder.add("des", String.class);
            //设置此数据存储的特征类型
            ds.createSchema(tBuilder.buildFeatureType());
            //设置编码
            ds.setCharset(Charset.forName("UTF-8"));
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
            SimpleFeature feature = writer.next();
            double x = 116.123; //X轴坐标
            double y = 39.345 ; //Y轴坐标
            Coordinate coordinate = new Coordinate(x, y);
            //Point point = new GeometryFactory().createPoint(coordinate);

            GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );

            WKTReader reader = new WKTReader(geometryFactory);

            Polygon polygon =(Polygon) reader.read("POLYGON((0 0,1 0,1 1,0 1,0 0 ))");


            feature.setAttribute("the_geom",polygon);
            feature.setAttribute("osm_id", 1234567890l);
            feature.setAttribute("name", "帅哥");
            feature.setAttribute("des", "爱美女");

            feature = writer.next();
          /*  x = 116.456;
            y = 39.678 ;
            coordinate = new Coordinate(x, y);
            point = new GeometryFactory().createPoint(coordinate);
            feature.setAttribute("the_geom",point);*/

             reader = new WKTReader(geometryFactory);

             polygon =(Polygon) reader.read("POLYGON ((2 0,3 0,3 1,2 1,2 0))");

            feature.setAttribute("the_geom",polygon);
            feature.setAttribute("osm_id", 1234567891l);
            feature.setAttribute("name", "美女");
            feature.setAttribute("des", "爱帅哥");
            writer.write();
            writer.close();
            ds.dispose();



            System.out.println("OK!");

        }
        catch (Exception ex){
           ex.printStackTrace();
        }
    }
}
